package gui5e_modules;

import java.io.File;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.Observable;
import java.util.Observer;

import controlP5.Button;
import controlP5.ControlEvent;
import controlP5.ControlFont;
import controlP5.ControllerInterface;
import controlP5.DropdownList;
import controlP5.Group;
import gui5e.*;

public class ModPreferences extends Module  implements Observer{

	// fields
	private Panel panel;
	private DropdownList listLang;
	private DropdownList listStyle;
	private Group lanG;
	
	// constructor
	public ModPreferences(Panel p){
		panel = p;
	}
	
	//methods
	public void setup(){
		  GUI.eventListener.add(this); // registering for getting events out of controlP5!!
		  lanG = panel.ctrl.addGroup("PREFERENCES").setBackgroundHeight(150);
		  GUI.styleGroup(lanG, "PREFERENCES");
		  // setup folders and watchers
		  GUI.gen = Preferences.create(this, GUI.PATH + "app-data/general"); 
		  GUI.lan = Preferences.create(this, GUI.PATH + "app-data/languages"); 
		  GUI.sty = Preferences.create(this, GUI.PATH + "app-data/styles");
		  //sty.watcher.requestStop();

		  listLang =  panel.ctrl.addDropdownList("LANGUAGE").setGroup(lanG).setPosition(10,30);
		  GUI.styleList(listLang, "LANGUAGE");
		  listStyle = panel.ctrl.addDropdownList("STYLE").setGroup(lanG).setPosition(145,30);
		  GUI.styleList(listStyle, "STYLE");
		  
		  panel.ctrlGroups.put("Toolbox_Preferences", lanG);
		  
		  // LOAD TOOLBOX PREFERENCES STUFF
		  //LANGUAGE
		  File[] files_ = GUI.lan.watcher.files;
		  Arrays.sort(files_);
		  int i = 0;
		  for(File f_:files_) {
			    listLang.addItem(f_.getName(), i);
			    i++;
		  }
		  listLang.setIndex(1);
		  GUI.lan.chose((int) listLang.getValue());
		  // STYLE
		  files_ =  GUI.sty.watcher.files;
		  Arrays.sort(files_);
		  i = 0;
		  for(File f_:files_) {
			    listStyle.addItem(f_.getName(), i);
			    i++;
		  }
		  //listStyle.setIndex(0);
		  GUI.sty.chose((int) listStyle.getValue());
	}
	
	public void setEvent(ControlEvent theEvent){
		switch(theEvent.getName()){
			case "LANGUAGE":GUI.lan.chose((int) listLang.getValue()); setLang(); break;
			case "STYLE": GUI.sty.chose((int) listStyle.getValue()); setStyles(); break;
    	}
	}
	    
	    private void updt_listLang(){
	    	if(listLang != null) {
	    		panel.isWaiting=true;
	    		int val_ =  (int) listLang.getValue();
	    		File[] files_ =  GUI.lan.watcher.files;
	        	Arrays.sort(files_);
	        	listLang.clear();
	        	int i=0;
	    		for(File f_:files_) {
	    	        listLang.addItem(f_.getName(), i);
	    	        i++;
	    	  	}
	    		// for refreshing the activa data of the gui
	    		GUI.lan.chose(val_);
	    		setLang();
	    		panel.isWaiting=false;
	    	}
		  }
	    
	    private void updt_listStyle(){
	    	if(listStyle != null) {
	    		panel.isWaiting=true;
	    		int val_ =  (int) listStyle.getValue();
	    		File[] files_ =  GUI.sty.watcher.files;
	        	Arrays.sort(files_);
	        	listStyle.clear();
	        	int i=0;
	    		for(File f_:files_) {
	    	        listStyle.addItem(f_.getName(), i);
	    	        i++;
	    	  	}
	    		// for refreshing the activa data of the gui
	    		GUI.sty.chose(val_);
	    		setStyles();
	    	    panel.isWaiting=false;
	    	}
		  }
	    
		public void update(Observable o, Object arg) {
			switch(arg.toString().split("\\?")[2]){
				case "languages": updt_listLang(); break;
				case "styles": updt_listStyle(); break;
			}
		}
		
		@SuppressWarnings("rawtypes")
		public void setLang(){
			for (Entry<ControllerInterface, String> item_: GUI.labels.entrySet()){
					if(GUI.lan.get(item_.getValue()) != null) 
						item_.getKey().setLabel(GUI.lan.get(item_.getValue())); 
					else 
						item_.setValue(item_.getKey().getName()); 
					switch(item_.getKey().getClass().getName()){
						case "controlP5.Group":GUI.styleGroup((Group) item_.getKey(), item_.getValue()); break;
						case "controlP5.DropdownList":GUI.styleList((DropdownList) item_.getKey(), item_.getValue()); break;
						case "controlP5.Button":GUI.styleButton((Button) item_.getKey(), item_.getValue(), "keep"); break;
				}	
			}
		}
		
		public void setStyles(){
			try{
				GUI.panelLeftWidth = GUI.sty.getInt("PANEL_LEFT_WIDTH");
				GUI.panelRightWidth = GUI.sty.getInt("PANEL_RIGHT_WIDTH");
				GUI.panelTopHeight = GUI.sty.getInt("PANEL_TOP_HEIGHT");
				GUI.panelBottomHeight = GUI.sty.getInt("PANEL_BOTTOM_HEIGHT");
				GUI.backgroundPanel = GUI.sty.getIntArray("BACKGROUND_PANEL");
				GUI.borderPanel = GUI.sty.getIntArray("BORDER_PANEL");
				GUI.background = GUI.sty.getIntArray("BACKGROUND");
				GUI.cp5_backgroundGroup = GUI.sty.getIntArray("CP5_BACKGROUNDGROUP");
				GUI.cp5_background = GUI.sty.getIntArray("CP5_BACKGROUND");
				GUI.cp5_active = GUI.sty.getIntArray("CP5_ACTIVE");
				GUI.cp5_caption = GUI.sty.getIntArray("CP5_CAPTION");
				GUI.cp5_foreground = GUI.sty.getIntArray("CP5_FOREGROUND");
				GUI.cp5_value = GUI.sty.getIntArray("CP5_VALUE");
				GUI.PanelSpeed = GUI.sty.getInt("PANEL_SPEED");
				GUI.cp5_setBarHeight = GUI.sty.getInt("CP5_SETBARHEIGHT");
				GUI.cp5_setWidth = GUI.sty.getInt("CP5_SETWIDTH");
				GUI.cp5_setPadding = GUI.sty.getIntArray("CP5_SETPADDING");
				GUI.fontA = new ControlFont(panel.parent_.createFont(GUI.sty.getArray("FONT_A")[0], Integer.parseInt(GUI.sty.getArray("FONT_A")[1]), Boolean.parseBoolean(GUI.sty.getArray("FONT_A")[2])));
				GUI.cp5_toUpperCase = Boolean.parseBoolean(GUI.sty.get("CP5_TOUPPERCASE"));
				panel.parent_.frameRate(GUI.sty.getInt("FRAMERATE"));	
				panel.ctrl.setColorValueLabel(panel.parent_.color(GUI.cp5_value[0],GUI.cp5_value[1],GUI.cp5_value[2],GUI.cp5_value[3]));
				panel.ctrl.setColorForeground(panel.parent_.color(GUI.cp5_foreground[0],GUI.cp5_foreground[1],GUI.cp5_foreground[2],GUI.cp5_foreground[3]));
				panel.ctrl.setColorCaptionLabel(panel.parent_.color(GUI.cp5_caption[0],GUI.cp5_caption[1],GUI.cp5_caption[2],GUI.cp5_caption[3]));
				panel.ctrl.setColorActive(panel.parent_.color(GUI.cp5_active[0],GUI.cp5_active[1],GUI.cp5_active[2],GUI.cp5_active[3]));
				panel.ctrl.setColorBackground(panel.parent_.color(GUI.cp5_background[0],GUI.cp5_background[1],GUI.cp5_background[2],GUI.cp5_background[3]));
				panel.ctrl.setFont(GUI.fontA);
				panel.ctrl.setUpdate(true);
				setLang(); //redrawing of ctrl items (barheight,size,...) --> iterates through all items via its name anyway!
			}catch (Exception e){	//Catch exception if any
				System.err.println("Error: " + e.getMessage()); 
			}
			
		}
}
